Load Libraries
library(tidyverse)
library(sf)
library(plotly)
library(hrbrthemes)
Load Data
# Import maps
landkreise_in_germany <- read_sf("data/landkreise/landkreise-in-germany.shp")
plz_gebiete <- read_sf("data/plz-2stellig/plz-2stellig.shp")
# Import tankstation data
stations_2019 <- read_csv("data/stations/stations.csv") %>%
mutate(post_2stellig = substring(post_code, 1, 2))
head(stations_2019)
summary(stations_2019)
## uuid name brand
## Length:15442 Length:15442 Length:15442
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
##
##
##
## street house_number post_code
## Length:15442 Length:15442 Length:15442
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
##
##
##
## city latitude longitude post_2stellig
## Length:15442 Min. : 0.00 Min. : 0.000 Length:15442
## Class :character 1st Qu.:49.39 1st Qu.: 8.026 Class :character
## Mode :character Median :50.97 Median : 9.297 Mode :character
## Mean :50.83 Mean : 9.596
## 3rd Qu.:52.17 3rd Qu.:11.067
## Max. :55.02 Max. :14.984
stations_2019 %>%
select(uuid, brand, city, longitude, latitude) %>%
head()
Get the top 10 brand names
top_10_brands <- stations_2019 %>%
count(brand) %>%
arrange(desc(n)) %>%
filter(!is.na(brand)) %>%
head(10) %>%
pull(brand)
top_10_brands
## [1] "ARAL" "Shell" "ESSO" "TOTAL" "AVIA"
## [6] "JET" "STAR" "Agip" "Raiffeisen" "HEM"
Plot Top Brands in Germany
# Filter stations and transform longitude and latitude
geo <- stations_2019 %>%
filter(brand %in% top_10_brands[1:5]) %>%
filter(latitude > 0 & longitude > 0) %>%
sample_frac(0.2) %>%
sf::st_as_sf(coords = c("longitude", "latitude"), crs=4326) %>%
sf::st_transform(crs=4326)
# Combine data with coordinates
station_coords <- cbind(geo, sf::st_coordinates(geo))
# Plot german map and points from station_cords on top
plt <- ggplot() +
geom_sf(data=landkreise_in_germany, alpha=0.2, colour="grey") +
geom_point(data=station_coords, aes(X, Y, fill=brand), alpha=0.8) +
hrbrthemes::theme_ipsum_tw()
plotly::ggplotly(plt)